
//加载方法
$(function (){
    $.ajax({
        url: "/DHMManufacture/numberManufactureTagGroup0",
        type:"get",
        success:function (data){
            $("#factureTag0").html(data);
        }
    })
    $.ajax({
        url: "/DHMManufacture/numberManufactureTag0",
        type:"get",
        success:function (data){
            $("#factureTag1").html(data);
        }
    })
    //模糊查询的方法，生成表格
    $("#mManufactureTable").bootstrapTable({
        method:"get",
        contentType:"application/json;charset=utf-8",
        url: "/DHMManufacture/queryByCheckMApply1",
        striped: true,
        pageNumber:1,
        pagination:true,
        sidePagination:"server",//设置服务端分页还是客户端分页
        pageSize:5,//页大小
        pageList:[5,9,13],//每页显示的条数
        queryParams:function (params) {
            return {
                pageNum: (params.offset / params.limit) + 1,
                pageSize:params.limit,
            }
        },
        columns:[
            {title:'点选',field:'id',align:'center',
                formatter:function (value,row,index) {
                    let input = "<input type='checkbox' class='checkedInput' value='"+value+"'>";
                    return input;
                }
            },
            {title:'生产计划编号',field:'applyId',align:'center'},
            {title:'产品编号',field:'productId',align:'center'},
            {title:'产品名称',field:'productName',align:'center'},
            {title:'描述',field:'productDescribe',align:'center'},
            {title:'数量',field:'amount',align:'center'},
            {title:'登记时间',field:'registerTime',align:'center'},
            {title:'审核时间',field:'checkTime',align:'center'},
            {title:'审核人',field:'checker',align:'center'},
        ]
    })
})


//生成派工单
function generateManufacture(){
    let checkedInput = $(".checkedInput:checked");
    if (checkedInput.length==0){
        alert("请先选择要派工的计划")
    }else{
        let flag=true;
        checkedInput.each(function (i,e){
            checkedInput.each(function (j,b){
                if ($(e).parent().next().next().html()!=$(b).parent().next().next().html()){
                    flag=false;
                }
            })
        })
        if (flag){
            //进入单页
            $(".div1").hide();
            $(".div2").show();
            let arr = [];
            checkedInput.each(function (i,e){
                arr[i]=$(e).val();
            })
            document.getElementById("mManufactureForm").reset();
            //查询产品数量和信息
            $.ajax({
                url:"/DHMApply/byIdMApplyList",
                type:"get",
                data:{
                    ids:arr.toString(),
                },
                dataType:"json",
                success:function (data){
                    $("input[name='designer']").val(parent.document.getElementsByClassName("user_name")[0].innerHTML);
                    $("input[name='register']").val(parent.document.getElementsByClassName("user_name")[0].innerHTML);
                    $("input[name='productId']").val(data[0].productId);
                    $("input[name='applyIdGroup']").val(arr);
                    $("input[name='productName']").val(data[0].productName);
                    $("textarea[name='productDescribe']").val(data[0].productDescribe);
                    //定义数量
                    let num=0;
                    for (let i = 0; i < data.length; i++) {
                        num+=parseInt(data[i].amount);
                    }
                    $("input[name='amount']").val(num);
                }
            })


        }else{
            alert("选择多个需要产品编号相同")
        }
    }
}

//点击预览
let procedureObject={};
let procedures=[];
function factureWatch(){
    let productIdd = $("input[name='productId']").val();
    $.ajax({
        url:"/DHMManufacture/DHQueryByProductId",
        type:"get",
        data: {
            productId:productIdd
        },
        dataType: "json",
        success:function (data){
            if (data==null){
                alert("对不起，该产品的工序设计或工序物料设计尚未完成，不能制定生产派工单，请返回。")
            }else if (data.designModuleTag=='2' && data.checkTag=='1'){
                $(".btn1").show();
                $(".btn2").hide();
                $(".table1").show();
                $("textarea[name='remark']").attr("readonly","readonly");
                procedureObject=data;

                $.ajax({
                    url:"/DHMManufacture/DHQueryByParentIdProcedureDetails",
                    type:"get",
                    data:{
                        parentId:data.id
                    },
                    dataType:"json",
                    success:function (data){
                        procedures=data;
                        let tr="";
                        for (let i = 0; i < data.length; i++) {
                            tr+="<tr>\n" +
                                "                    <td>"+data[i].detailsNumber+"</td>\n" +
                                "                    <td>"+data[i].procedureName+"</td>\n" +
                                "                    <td>"+data[i].procedureId+"</td>\n" +
                                "                    <td>"+data[i].procedureDescribe+"</td>\n" +
                                "                    <td>"+data[i].labourHourAmount+"</td>\n" +
                                "                    <td>"+data[i].costPrice+"</td>\n" +
                                "                    <td>"+data[i].subtotal+"</td>\n" +
                                "                    <td><a href='#' onclick='watchModule("+data[i].id+")'" +
                                " data-toggle=\"modal\" data-target=\"#myModal\">查看物料</a></td>\n" +
                                "                </tr>";
                        }
                        $("#tbodyForTr").html(tr);

                        //将数据组装好
                        for (let i = 0; i < procedures.length; i++) {
                            $.ajax({
                                url:"/DHMManufacture/queryByParentIdModule",
                                type:"get",
                                data:{
                                    parentId:procedures[i].id
                                },
                                dataType:"json",
                                success:function (data){
                                    procedures[i].mProcedureModules=[];
                                    procedures[i].mProcedureModules=data;
                                    procedures[i].procedureMark=procedures[i].procedureDescribe;
                                    procedures[i].realLabourHourAmount='0.0';
                                    procedures[i].realSubtotal='0.0';
                                    procedures[i].realModuleSubtotal='0.0';
                                    procedures[i].demandAmount= $("input[name='amount']").val();
                                    procedures[i].procedureFinishTag= '0';
                                    procedures[i].procedureTransferTag= '0';
                                    procedures[i].parentId= 0;
                                }
                            })
                        }
                    }
                })
            }else{
                alert("对不起，该产品的工序设计或工序物料设计尚未完成，不能制定生产派工单，请返回。")
            }
        },
        error:function (err){
            alert("对不起，该产品的工序设计或工序物料设计尚未完成，不能制定生产派工单，请返回。")
        }
    })
}

//返回列表
function returnList(){
    $(".div1").show();
    $(".div2").hide();
}

//点击取消
function cancelFacture(){
    $(".btn1").hide();
    $(".btn2").show();
    $(".table1").hide();
    $("textarea[name='remark']").removeAttr("readonly");
}

//点击提交派工单
function saveFacture(){
    if (confirm("确定提交该派工单吗？")){
        let formData = getFormData($("#mManufactureForm"));
        let parse = JSON.parse(formData);
        parse.labourCostPriceSum=procedureObject.costPriceSum*parse.amount;
        parse.realLabourCostPriceSum='0.0';
        parse.moduleCostPriceSum=procedureObject.moduleCostPriceSum*parse.amount;
        parse.realModuleCostPriceSum='0.0';
        parse.mProcedures=procedures;
        $.ajax({
            url:"/DHMManufacture/addDHMManufacture",
            type:"post",
            data:JSON.stringify(parse),
            contentType: "application/json;charset=utf-8",
            success:function (data){
                alert("提交成功")
                $("#mManufactureTable").bootstrapTable("refresh");
                $(".div1").show();
                $(".div2").hide();
                $(".table1").hide();
            }
        })
    }
}

//查看物料
function watchModule(ModuleParentId){

    $.ajax({
        url:"/DHMManufacture/queryByParentIdModule",
        type:"get",
        data:{
            parentId:ModuleParentId
        },
        dataType:"json",
        success:function (data){
            let tr="";
            for (let i = 0; i < data.length; i++) {
                let unit = data[i].amountUnit;
                if (unit==undefined){
                    unit="";
                }
                tr+="<tr>\n" +
                    "                    <td>"+data[i].detailsNumber+"</td>\n" +
                    "                    <td>"+data[i].productName+"</td>\n" +
                    "                    <td>"+data[i].productId+"</td>\n" +
                    "                    <td>"+data[i].productDescribe+"</td>\n" +
                    "                    <td>"+data[i].amount+"</td>\n" +
                    "                    <td>"+unit+"</td>\n" +
                    "                    <td>"+data[i].costPrice+"</td>\n" +
                    "                    <td>"+data[i].subtotal+"</td>\n" +
                    "                </tr>";
            }
            $("#moduleWatch").html(tr);
        }
    })
}